package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.LGoods;
import com.ruoyi.system.template.LGoodsTemplate;
import com.ruoyi.system.template.LGuestTemplate;
import com.ruoyi.system.vo.LGuestParam;
import com.ruoyi.system.vo.LGuestVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LGuest;
import com.ruoyi.system.service.ILGuestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户Controller
 *
 * @author zyb
 * @date 2022-01-02
 */
@RestController
@RequestMapping("/system/guest")
public class LGuestController extends BaseController
{
    @Autowired
    private ILGuestService lGuestService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:guest:list')")
    @GetMapping("/list")
    public TableDataInfo list(LGuestVo lGuest)
    {
        startPage();
        List<LGuest> list = lGuestService.selectLGuestList(lGuest);
        return getDataTable(list);
    }

    /**
     * 查询全部客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:guest:list')")
    @GetMapping("/listall")
    public TableDataInfo listAll(LGuestVo lGuest)
    {
        List<LGuest> list = lGuestService.selectLGuestList(lGuest);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:guest:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LGuestVo lGuest)
    {
        List<LGuest> list = lGuestService.selectLGuestList(lGuest);
        ExcelUtil<LGuest> util = new ExcelUtil<LGuest>(LGuest.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:guest:query')")
    @GetMapping(value = "/{gid}")
    public AjaxResult getInfo(@PathVariable("gid") Long gid)
    {
        return AjaxResult.success(lGuestService.selectLGuestByGid(gid));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('system:guest:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LGuest lGuest)
    {
        redisCache.deleteObject("lGuest");
        return toAjax(lGuestService.insertLGuest(lGuest));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('system:guest:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LGuest lGuest)
    {
        redisCache.deleteObject("lGuest");
        return toAjax(lGuestService.updateLGuest(lGuest));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('system:guest:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gids}")
    public AjaxResult remove(@PathVariable Long[] gids)
    {
        redisCache.deleteObject("lGuest");
        return toAjax(lGuestService.deleteLGuestByGids(gids));
    }

    @GetMapping("/list/all")
    public AjaxResult listAll()
    {
        List<LGuest> list = redisCache.getCacheList("lGuest");
        if(list.size()==0){
            LGuestVo lGuest=new LGuestVo();
            lGuest.setClosed(0);
            list = lGuestService.selectLGuestList(lGuest);
            redisCache.setCacheList("lGuest",list);
        }

        return AjaxResult.success(list);
    }

    @GetMapping("/bind/user/list")
    public AjaxResult bindUserList()
    {
        List<SysUser> list = lGuestService.bindUserList();

        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:guest:binduser')")
    @PostMapping("/bind/user")
    @Log(title = "客户绑定用户信息", businessType = BusinessType.UPDATE)
    public AjaxResult bindUser(@RequestBody  LGuest lGuest)
    {
        redisCache.deleteObject("lGuest");
        Map<String,Object> map = lGuestService.bindUser(lGuest);
        return AjaxResult.success(map);
    }
    @PreAuthorize("@ss.hasPermi('system:guest:generateuser')")
    @PostMapping("/generate/user")
    @Log(title = "客户生成用户信息", businessType = BusinessType.UPDATE)
    public AjaxResult generateUser(@RequestBody  LGuest lGuest)
    {
        redisCache.deleteObject("lGuest");
        Map<String,Object> map = lGuestService.generateUser(lGuest.getGid());
        return AjaxResult.success(map);
    }
    @PreAuthorize("@ss.hasPermi('system:guest:generateuser')")
    @PostMapping("/batch/generate/user")
    @Log(title = "客户生成用户信息", businessType = BusinessType.UPDATE)
    public AjaxResult generateUser(@RequestBody Long[] gids)
    {
        redisCache.deleteObject("lGuest");
        return AjaxResult.success(lGuestService.batchGenerateUser(gids));
    }
    @PreAuthorize("@ss.hasPermi('system:guest:generateuser')")
    @PostMapping("/batch/generate/user/all")
    @Log(title = "客户生成用户信息", businessType = BusinessType.UPDATE)
    public AjaxResult generateUser()
    {
        redisCache.deleteObject("lGuest");
        return AjaxResult.success(lGuestService.batchGenerateUserAll());
    }
    //微信接口
    //客户信息
    @GetMapping("/wx/getinfo")
    public AjaxResult wxGetInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return AjaxResult.success(lGuestService.selectByUserId(loginUser.getUserId()));
    }


    @Log(title = "客户导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:guest:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LGuestTemplate> util = new ExcelUtil<LGuestTemplate>(LGuestTemplate.class);
        List<LGuestTemplate> guestTemplateList = util.importExcel(file.getInputStream());
        String message = lGuestService.importGuest(guestTemplateList, updateSupport);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<LGuestTemplate> util = new ExcelUtil<LGuestTemplate>(LGuestTemplate.class);
        util.importTemplateExcel(response, "客户数据");
    }
    @PostMapping("/reportGuest")
    public AjaxResult reportGuest(LGuestParam guestParam)
    {
        return AjaxResult.success(lGuestService.reportGuest(guestParam));
    }
}
